package com.yjt.fresh.fragment;

import android.os.Bundle;
import android.view.View;

import com.yjt.fresh.R;
import com.yjt.fresh.base.BaseSimpleFragment;
import com.yjt.fresh.bean.MessageEvent;
import com.yjt.fresh.constant.MyConstants;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;
import butterknife.ButterKnife;
import im.delight.android.webview.AdvancedWebView;

/**
 * 商品详情的商品
 * Created by mcc on 2018/6/15.
 */

public class FrgProductDetailDetail extends BaseSimpleFragment {
    @BindView(R.id.wbAppFrgProductDetail)
    AdvancedWebView mWebView;

    @Override
    protected int getContentView() {
        return R.layout.app_atyproductdetail_frg_detail;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        ButterKnife.bind(this, view);

    }

    @Override
    protected void initEvents() {
        EventBus.getDefault().register(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent data) {
        if (data.getRecode() == MyConstants.PRODUCT_DETAIL_CONTENT) {
            String html = "";
            try {
                Document doc = Jsoup.parse(data.getMsg());
                Elements elements = doc.getElementsByTag("img");
                for (Element element : elements) {
                    element.attr("width", "100%").attr("height", "auto");
                }

                html = doc.toString();
            } catch (Exception e) {
                html = data.getMsg();
            }
            mWebView.loadHtml(html);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
